package cn.sinjet.carassist;

import java.util.LinkedList;
import java.util.List;

//import com.amap.navi.BaiduTTSModel;
import com.amap.navi.NaviModel;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.Handler;
import android.util.Log;
import cn.sinjet.communication.bluetooth.BluetoothModel;
import cn.sinjet.communication.broadcast.SinjetBroadcastHandler;
//import cn.sinjet.mediaplayer.MediaApplication;
import cn.sinjet.model.carassist.AppModel;
import cn.sinjet.model.carassist.CarAssistService;
import cn.sinjet.model.carsystem.CarModel;
import cn.sinjet.phone.PhoneModel;
import cn.sinjet.utils.OsUtils;
import cn.sinjet.viewmodel.ViewModel;

public class SinjetApplication extends Application { 
        private static SinjetApplication  mMediaApp = null;
		private List<Activity> activities = new LinkedList<Activity>();
		private static SinjetApplication instance;
		SinjetBroadcastHandler mSinjetBroadcastReceiver;
		MessageClient mMessageClient = null;
		public static SinjetApplication getInstance(){ 
			return instance; 
		} 
		//添加Activity到容器中 
		public void addActivity(Activity activity){ 
			activities.add(activity);
		} 
		public void deleteActivity(Activity activity){
			activities.remove(activity);
		}
		
		public MessageClient getMsgClient(){
			return mMessageClient;
		}
		
		//finish 
		public void exit() 
		{ 
			mSinjetBroadcastReceiver.uninitReceiver();
			NaviModel.getInstance().deInit();
			CarModel.getInstance().deInit();
			AppModel.getInstance().deInit();
			PhoneModel.getIns(this).uninit();
//			TTSController.getInstance(this).stopSpeaking();
//			TTSController.getInstance(this).destroy();
//			BaiduTTSModel.getInstance(this).deInit();
			BluetoothModel.getInstance().clearBTMode();
			
			for(Activity activity:activities){
				  activity.finish();
			}
			activities.clear();
			
			requestMediaExit();
			Intent intentS = new Intent(getApplicationContext(),CarAssistService.class);
	        stopService(intentS);
	        
			System.exit(1);
		} 
		
		
		private void requestMediaExit(){
			String ACTION_EXIT_PROCESS = "cn.sinjet.action.MEDIA_EXIT_PROCESS";
			Intent intent = new Intent(ACTION_EXIT_PROCESS);
			sendBroadcast(intent);
		}
		
		@Override
		public void onCreate() {
		// TODO Auto-generated method stub
		  super.onCreate();
		  String processName = OsUtils.getProcessName(this, android.os.Process.myPid());
			
	        if (processName != null) {
	        	Log.d("app", "onCreate process name:"+processName);
	            if(processName.equals("com.mgroup.wazehelper")){
	            /*	CrashHandler crashHandler = CrashHandler.getInstance();
	    	        crashHandler.init(getApplicationContext());*/
	                instance = SinjetApplication.this;
	                new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							   initApp();    	
						}
					},1000);
	            }/*else if(processName.equals("cn.sinjet.mediaplayer")){
	            	mMediaApp = SinjetApplication.this;
//	            	MediaApplication.getInstance().onCreate(mMediaApp);
	            }*/
	        }
	        
		}
		
		
		@Override
		public void onTerminate() {
		// TODO Auto-generated method stub
		   super.onTerminate();
		   mSinjetBroadcastReceiver.uninitReceiver();
		   for(Activity activity:activities){
				  activity.finish();
			}
			activities.clear();
		}
  
        private void initApp(){
            ViewModel.getIns();
            AppModel.getInstance().init(this);
            NaviModel.getInstance().init(this);
            
            mMessageClient = new MessageClient(this);
            mMessageClient.init(this);
        	mSinjetBroadcastReceiver = new SinjetBroadcastHandler(this);
        	mSinjetBroadcastReceiver.initReceiver();
        	CarModel.getInstance().init(this);
        	PhoneModel.getIns(this);
 			Log.d("app", "App onCreate initApp done");
        }
         
        
	@Override
    public void onLowMemory() {
    // TODO Auto-generated method stub
    super.onLowMemory();
      Log.d("test","onLowMemory");
    }
      @Override
    public void onTrimMemory(int level) {
    // TODO Auto-generated method stub
    super.onTrimMemory(level);
      Log.d("test","onTrimMemory:"+level);
        if(level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
        	AppModel.getInstance().storeAppData();
    }
      
   @Override
	public void onConfigurationChanged(Configuration newConfig) {
	// TODO Auto-generated method stub
	    super.onConfigurationChanged(newConfig);
	    Log.d("ui", "orientation:"+newConfig.orientation);
	    if(mFloatMenu != null) mFloatMenu.onOrientationChanged(newConfig.orientation);
	}
	
   FloatMenu mFloatMenu = null;
   public void showFloatMenu(boolean show){
	   if(show){
			   new Handler().postDelayed(new Runnable() {
		 			@Override
		 			public void run() {
		 				// TODO Auto-generated method stub
		 				if(mFloatMenu == null)
		 				      mFloatMenu = new FloatMenu(SinjetApplication.this);
		 				mFloatMenu.show();
		//	  	        floatMenu.createFloatMenu(100, 100);
		 			}
		 		}, 1000);
		   }else{
			   if(mFloatMenu != null){
				   mFloatMenu.hide();
//				   mFloatMenu = null;
			   }
		   }
	   }
   
     int mMediaFocuse = AudioManager.AUDIOFOCUS_LOSS;
     public void onMyMediaFocusSate(int state){
    	 mMediaFocuse = state;
     }
     public boolean isMediaFocuseOn(){
    	 return (mMediaFocuse == AudioManager.AUDIOFOCUS_LOSS)?false:true;
     }
     
} 
